use std::fmt;
// 引入错误响应模块
// 引入错误响应模块，用于处理各类错误响应
pub mod error_response;

/// Error types for Vertica client
#[derive(Debug)]
pub enum VerticaError {
    /// 连接错误，包含错误信息
    Connection(String),
    /// 认证错误，包含错误信息
    Authentication(String),
    /// 查询错误，包含错误信息
    Query(String),
    /// 协议错误，包含错误信息
    Protocol(String),
    /// 配置错误，包含错误信息
    Config(String),
    /// IO 错误，包含标准库的 IO 错误
    Io(std::io::Error),
    /// UTF-8 解码错误，包含标准库的 UTF-8 错误
    Utf8(std::str::Utf8Error),
    /// 从 UTF-8 转换错误，包含标准库的从 UTF-8 转换错误
    FromUtf8(std::string::FromUtf8Error),
    /// 解析整数错误，包含标准库的解析整数错误
    ParseInt(std::num::ParseIntError),
    /// 解析浮点数错误，包含标准库的解析浮点数错误
    ParseFloat(std::num::ParseFloatError),
    /// 解析布尔值错误，包含标准库的解析布尔值错误
    ParseBool(std::str::ParseBoolError),
    /// 类型转换错误，包含错误信息
    TypeConversion(String),
    /// TLS 错误，包含错误信息
    Tls(String),
    /// 操作超时
    Timeout,
    /// 操作被取消
    Cancelled,
}
impl fmt::Display for VerticaError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            VerticaError::Connection(msg) => write!(f, "Connection error: {}", msg),
            VerticaError::Authentication(msg) => write!(f, "Authentication error: {}", msg),
            VerticaError::Query(msg) => write!(f, "Query error: {}", msg),
            VerticaError::Protocol(msg) => write!(f, "Protocol error: {}", msg),
            VerticaError::Config(msg) => write!(f, "Configuration error: {}", msg),
            VerticaError::Io(e) => write!(f, "IO error: {}", e),
            VerticaError::Utf8(e) => write!(f, "UTF-8 error: {}", e),
            VerticaError::FromUtf8(e) => write!(f, "From UTF-8 error: {}", e),
            VerticaError::ParseInt(e) => write!(f, "Parse int error: {}", e),
            VerticaError::ParseFloat(e) => write!(f, "Parse float error: {}", e),
            VerticaError::ParseBool(e) => write!(f, "Parse bool error: {}", e),
            VerticaError::TypeConversion(msg) => write!(f, "Type conversion error: {}", msg),
            VerticaError::Tls(msg) => write!(f, "TLS error: {}", msg),
            VerticaError::Timeout => write!(f, "Operation timed out"),
            VerticaError::Cancelled => write!(f, "Operation cancelled"),
        }
    }
}

impl std::error::Error for VerticaError {}

impl From<std::io::Error> for VerticaError {
    fn from(err: std::io::Error) -> Self {
        VerticaError::Io(err)
    }
}

impl From<std::str::Utf8Error> for VerticaError {
    fn from(err: std::str::Utf8Error) -> Self {
        VerticaError::Utf8(err)
    }
}

impl From<std::string::FromUtf8Error> for VerticaError {
    fn from(err: std::string::FromUtf8Error) -> Self {
        VerticaError::FromUtf8(err)
    }
}

impl From<url::ParseError> for VerticaError {
    fn from(err: url::ParseError) -> Self {
        VerticaError::Config(format!("Invalid URL: {}", err))
    }
}

impl From<std::num::ParseIntError> for VerticaError {
    fn from(err: std::num::ParseIntError) -> Self {
        VerticaError::ParseInt(err)
    }
}

impl From<std::num::ParseFloatError> for VerticaError {
    fn from(err: std::num::ParseFloatError) -> Self {
        VerticaError::ParseFloat(err)
    }
}

impl From<std::str::ParseBoolError> for VerticaError {
    fn from(err: std::str::ParseBoolError) -> Self {
        VerticaError::ParseBool(err)
    }
}

/// Result type for Vertica operations
pub type Result<T> = std::result::Result<T, VerticaError>;